/**
 * Created by developer on 17-3-1.
 */
$shady = $(".masking");
var $id;
var $status;
$(function () {
    $(".add-region").on("click", function () {
        $("#map").empty();
        modifyDisplayFlex($(".container-region-new"));
        makeMap();
        $(".jvectormap-zoomin").css("display","none");
        $(".jvectormap-zoomout").css("display","none");
        $("#save_add_next").css("display", "inline-block");
    });
    
    $(".container-region-new").find(".cancel").on("click", function () {
        modifyDisplayNone($(".container-region-new"));
    });
    
    $(".list .list-item").on("click", function () {
        var currentId = $(this).prop("id");
        modifySliderStatus(modifyStatus(currentId), currentId);
    });

    $(".aside-slider-content").find(".cancel").on("click", function () {
        $(".aside-slider-content").animate({right:"-400px"});
    });

    $("#edit_region").on("click", function () {
        $("#map").empty();
        modifyDisplayFlex($(".container-region-new"));
        makeMap();
        $(".jvectormap-zoomin").css("display","none");
        $(".jvectormap-zoomout").css("display","none");
        $("#save_add_next").css("display", "none");
    });

    $("#delete_region").on("click",function () {
        modifyDisplayFlex($(".container-delete-region"));
    });

    $(".container-delete-region").find(".cancel").on("click", function () {
        modifyDisplayNone($(".container-delete-region"));
    });
});

function makeMap() {
    /*id为中国省份标识符，name为对应省份名称，两者固定。event为活动情况，
     　　url为活动列表页面地址，数据可后台动态生成，这两项有活动的地区填写，无则留空。*/
    var dataStatus = [
        {id: "CN-32", name: "江苏"},
        {id: "CN-52", name: "广州"},
        {id: "CN-53", name: "云南"},
        {id: "CN-50", name: "重庆"},
        {id: "CN-51", name: "四川"},
        {id: "CN-31", name: "上海"},
        {id: "CN-54", name: "西藏"},
        {id: "CN-33", name: "浙江"},
        {id: "CN-15", name: "内蒙古"},
        {id: "CN-14", name: "山西"},
        {id: "CN-", name: "福建"},
        {id: "CN-12", name: "天津"},
        {id: "CN-13", name: "河北"},
        {id: "CN-11", name: "北京"},
        {id: "CN-34", name: "安徽"},
        {id: "CN-36", name: "江西"},
        {id: "CN-37", name: "山东"},
        {id: "CN-41", name: "河南"},
        {id: "CN-43", name: "湖南"},
        {id: "CN-42", name: "湖北"},
        {id: "CN-45", name: "广西"},
        {id: "CN-44", name: "广东"},
        {id: "CN-46", name: "海南"},
        {id: "CN-65", name: "新疆"},
        {id: "CN-64", name: "宁夏"},
        {id: "CN-63", name: "青海"},
        {id: "CN-62", name: "甘肃"},
        {id: "CN-61", name: "陕西"},
        {id: "CN-23", name: "黑龙江"},
        {id: "CN-22", name: "吉林"},
        {id: "CN-21", name: "辽宁"}
    ];

    var ChinaMap = new jvm.Map({
        map: 'cn_mill',
        container: $("#map"),
        zoomOnScroll:false,
        backgroundColor:"white",
        regionsSelectable: true,
        markersSelectable: true,
        regionStyle: {
            initial: {
                fill: '#39ac90',
                "fill-opacity": 1,
                stroke: '#d7d7d7',
                "stroke-width": 0.3,
                "stroke-opacity": 1
            },
            hover: {
                "fill-opacity": 0.8,
                cursor: 'pointer'
            },
            selected: {
                fill: 'yellow'
            },
            selectedHover: {}
        },
        // markers: [
        //     {latLng: [23.804, 120.961454], name: "台湾"},
        //     {latLng: [22.281089, 114.171994], name: "香港"},
        //     {latLng: [22.192961, 113.5494], name: "澳门"}
        // ],
        // markerStyle: {
        //     initial: {
        //         fill: '#F8E23B',
        //         stroke: '#383f47'
        //     },
        //     hover: {
        //         "fill-opacity": 0.8,
        //         "cursor":"pointer"
        //     }
        // },
        //显示各地区名称和活动
        onRegionTipShow: function (event, el, code) {
            $.each(dataStatus, function (i, items) {
                if (code == items.id) {
                    el.html(items.name);
                    el.css("z-index", "3");
                }
                console.log(el);
            });
        },
        onRegionSelected: function (event, code) {
            var currentRegion = ChinaMap.tip.text(),
                selectedTargetIndex = selectedTargets.indexOf(currentRegion);
            if( selectedTargetIndex >= 0){
                selectedTargets.splice(selectedTargetIndex,1)
            }else{
                selectedTargets.push(currentRegion);
            }
            console.log(currentRegion);
            console.log(ChinaMap.getSelectedRegions());
        },
        onMarkerSelected: function (event, code) {
            var currentMaker = ChinaMap.markers[code].config.name,
                selectedTargetIndex = selectedTargets.indexOf(currentMaker);
            if( selectedTargetIndex >= 0){
                selectedTargets.splice(selectedTargetIndex,1);
            }else{
                selectedTargets.push(currentMaker);
            }
            console.log(currentMaker);
            console.log(ChinaMap.getSelectedRegions());
        }
    });
}

function modifyStatus(currentId) {

    console.log(currentId);
    if ($id == currentId) {
        $status = "close";
        $id = "";
    } else {
        $status = "open";
        $id = currentId;
    }
    return $status;
}

function modifySliderStatus($status, currentId) {
    console.log("error");
    console.log($status);
    if ($status == "close") {
        $(".aside-slider-content").animate({right: "-400px"});
    } else if ($status == "open"){
        $(".aside-slider-content").animate({right:0});
    }
}


//修改css样式display为flex
function modifyDisplayFlex(obj) {
    obj.css("display", "flex");
    $shady.css("display", "block");
}
//修改css样式display为none
function modifyDisplayNone(obj) {
    obj.css("display", "none");
    $shady.css("display", "none");
}